
export let serialPort = {
	socket: null,
	connected: function (callback) {
		console.log("connectting");
		var that = this;
		that.socket = new WebSocket("ws://127.0.0.1:8083");

		that.socket.onclose = function () {
			console.log("web close");
		};
		that.socket.onerror = function (error) {
			console.log("web error: " + error);
		}
		that.socket.onopen = function (data) {
			console.log("打开。。。。。")
			var message = {
				"alias": "tp_spb_01",
				"register": 1,
				"end": ""
			};
			that.socket.send(JSON.stringify(message));
			that.sendMessage(callback)
		}
	},
	removes: function () {
		if (this.socket != null) {
			if (0 != this.socket.readyState) {
				this.socket.close();
			}
		}
	},
	sendMessage: function (callback) {
		this.socket.onmessage = function (data) {
			callback(data.data)
		}
	},
	controlDoor: function (switchs, callback) {
		var message = {
			"alias": "tp_spb_01",
			"command": "control_door",
			"data": {
				"onoff": switchs    //1 开门，0 关门
			},
			"end": "",
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	},
	queryDoor: function (callback) {
		var message = {
			"alias": "tp_spb_01",
			"command": "query_door",
			"end": ""
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	},
	setDoorNotify: function (userData, callback) {
		var message = {
			"alias": "tp_spb_01",
			"command": "start_door_notify",
			"data": {
				"user_data": userData
			},
			"end": ""
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	},
	pushWegData: function (userId, callback) {
		var message = {
			"alias": "tp_spb_01",
			"command": "push_weg_data",
			"data": {
				"user_id": userId
			},
			"end": ""
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	},
	controlLed: function (onoff, callback) {
		var message = {
			"alias": "tp_spb_01",
			"command": "control_led",
			"data": {
				"onoff": onoff
			},
			"end": ""
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	},
	queryLed: function (callback) {
		var message = {
			"alias": "tp_spb_01",
			"command": "query_led",
			"end": ""
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	},
	startLedNotify: function (interval, userData, callback) {
		var message = {
			"alias": "tp_spb_01",
			"command": "start_led_notify",
			"data": {
				"time": interval,
				"user_data": userData
			},
			"end": ""
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	},
	stopLedNotify: function (callback) {
		var message = {
			"alias": "tp_spb_01",
			"command": "stop_led_notify",
			"end": ""
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	}
}
export let faceRecognition = {
	socket: null,
	connected: function (callback) {
		console.log("connectting");
		var that = this;
		that.socket = new WebSocket("ws://127.0.0.1:8083");
		that.socket.onclose = function () {
			console.error("web close");
		};
		that.socket.onerror = function (error) {
			console.error("web error: " + error);
		}
		that.socket.onopen = function (data) {
			var message = {
				"alias": "face_camera_dvt",
				"register": 1,
				"end": ""
			};
			that.socket.send(JSON.stringify(message));
			that.sendMessage(callback);
		}
	},
	removes: function () {
		if (this.socket != null) {
			if (0 != this.socket.readyState) {
				this.socket.close();
			}
		}
	},
	sendMessage: function (callback) {
		this.socket.onmessage = function (data) {
			callback(data.data)
		}
	},
	startFaceDetection: function (width, height, callback) {
		var message = {
			"alias": "face_camera_dvt",
			"command": "start_face_detection",
			"data": {
				"width": width,
				"height": height
			},
			"end": ""

		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	},
	stopFaceDetection: function (callback) {
		var message = {
			"alias": "face_camera_dvt",
			"command": "stop_face_detection",
			"end": ""
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	},
	initFaceLib: function (callback) {
		var message = {
			"alias": "face_camera_dvt",
			"command": "init_face_lib",
			"end": ""
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	},
	startSnap: function (width, height, type, user_data, callback) {
		var message = {
			"alias": "face_camera_dvt",
			"command": "start_face_snap",
			"data": {
				"width": width,
				"height": height,
				"type": type,
				"user_data": user_data
			},
			"end": ""
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	},
	stopSnap: function (callback) {
		var message = {
			"alias": "face_camera_dvt",
			"command": "stop_face_snap",
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	},
	pushFace: function (base64Data, userId, type, callback) {
		var message = {
			"alias": "face_camera_dvt",
			"command": "push_face",
			"data": {
				"base64Data": base64Data,
				"type": type,
				"user_id": userId
			},
			"end": ""
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	},
	getFaceCount: function (callback) {
		var message = {
			"alias": "face_camera_dvt",
			"command": "get_face_count",
			"end": ""
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	},
	queryUserByIndex: function (index, callback) {
		var message = {
			"alias": "face_camera_dvt",
			"command": "query_user_by_index",
			"data": {
				"index": index
			},
			"end": ""
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	},
	queryFaceByUser: function (userId, callback) {
		var message = {
			"alias": "face_camera_dvt",
			"command": "query_face_by_user",
			"data": {
				"user_id": userId
			},
			"end": ""
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	},
	popuFace: function (userId, callback) {
		var message = {
			"alias": "face_camera_dvt",
			"command": "del_face",
			"data": {
				"user_id": userId
			},
			"end": ""
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	},
	startRecog: function (userId, callback) {
		var message = {
			"alias": "face_camera_dvt",
			"command": "start_recog",
			"data": {
				"user_id": userId
			},
			"end": ""
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	},
	stopRecog: function (callback) {
		var message = {
			"alias": "face_camera_dvt",
			"command": "stop_recog",
			"end": ""
		};
		this.socket.send(JSON.stringify(message));
		this.sendMessage(callback)
	}

}
